package com.synchrona.pluginManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.synchrona.account.Account;
import com.synchrona.common.TinyURLUtils;
import com.synchrona.dataProcessor.DataProcessor;

public class FacebookPlugin {

	//private static String newStatus = null;
	public FacebookPlugin() {

	}

	public void runFB(String status, Account account) {
		//newStatus = status;
		try {

			System.out.println("Facebook Status: " + status);
			HttpClient httpclient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet("http://www.facebook.com/login.php");
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String responseStr = EntityUtils.toString(entity);
			}
			
			HttpPost httpost = new HttpPost("http://www.facebook.com/login.php");
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("email", account.getAccountEmail()));
			nvps.add(new BasicNameValuePair("pass", account.getServicePassword()));
			nvps.add(new BasicNameValuePair("login", ""));
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
			HttpResponse loginPostResponse = httpclient.execute(httpost);
			entity = loginPostResponse.getEntity();
			if (entity != null) {
				String responseStr = EntityUtils.toString(entity);
			}
			HttpGet loginGet = new HttpGet("http://www.facebook.com/home.php");
			response = httpclient.execute(loginGet);
			entity = response.getEntity();
			System.out.println("Facebook: Login form post: "
					+ response.getStatusLine());
			String responseStr = null;
			if (entity != null) {
				Thread.sleep(5000);
				responseStr = EntityUtils.toString(entity);
				entity.consumeContent();
			}
			
			String uid = null;
			List<Cookie> finalCookies = ((AbstractHttpClient) httpclient)
					.getCookieStore().getCookies();
			if (finalCookies.isEmpty()) {
				System.out.println("Facebook: None");
			} else {
				for (int i = 0; i < finalCookies.size(); i++) {

					if (finalCookies.get(i).getName().equals("c_user"))
						uid = finalCookies.get(i).getValue();
					if (uid != null) {
					}
				}
			}
			String post_form_id = "";
			String postFormIDPrefix = "<input type=\"hidden\" id=\"post_form_id\" name=\"post_form_id\" value=\"";
			int formIdBeginPos = responseStr.indexOf(postFormIDPrefix)
					+ postFormIDPrefix.length();
			if (formIdBeginPos < postFormIDPrefix.length()) {
				// System.out.println("Facebook: Error: Can't find post form ID!");

			} else {
				post_form_id = responseStr.substring(formIdBeginPos,
						formIdBeginPos + 32);
			}
			httpost = new HttpPost("http://www.facebook.com/ajax/updatestatus.php");
			List<NameValuePair> nvps2 = new ArrayList<NameValuePair>();
			String newStatus = status;
			System.out.println(newStatus);
			BasicNameValuePair statusPair = new BasicNameValuePair("status", newStatus);
			nvps2.add(statusPair);
			nvps2.add(new BasicNameValuePair("profile_id", uid));
			nvps2.add(new BasicNameValuePair("home_tab_id", "1"));
			nvps2.add(new BasicNameValuePair("test_name",
					"INLINE_STATUS_EDITOR"));
			nvps2.add(new BasicNameValuePair("action", "HOME_UPDATE"));
			nvps2.add(new BasicNameValuePair("post_form_id", post_form_id));
			nvps2.add(new BasicNameValuePair("ecdc", "false"));
			nvps2.add(new BasicNameValuePair("link", "false"));
			nvps2.add(new BasicNameValuePair("post_form_id_source",
					"AsynxRequest"));
			System.out.println("Facebook: @executeMethod setStatusMessage() ing ...");
			// we don't care the response string now
			 //httpclient.execute("http://www.facebook.com", "/updatestatus.php", nvps);
			httpost.setEntity(new UrlEncodedFormEntity(nvps2, HTTP.UTF_8));
			// execute postMethod
			HttpResponse postResponse = httpclient.execute(httpost);
			entity = postResponse.getEntity();
			//System.out.println("Facebook: facebookPostMethod: "
			//		+ postResponse.getStatusLine());
			if (entity != null) {
				responseStr = EntityUtils.toString(entity);
				// System.out.println("Facebook: "+responseStr);
				entity.consumeContent();
			}
			loginGet = new HttpGet(
					"http://www.facebook.com/home.php?#/profile.php?v=info&ref=profile&id=100000570500743");
			response = httpclient.execute(loginGet);
			entity = response.getEntity();
			// System.out.println("Facebook: Login form post: " +
			// response.getStatusLine());
			responseStr = null;
			if (entity != null) {
				Thread.sleep(5000);
				responseStr = EntityUtils.toString(entity);
				// writefile(responseStr);
				entity.consumeContent();
			}

			httpost = new HttpPost("http://www.facebook.com/profile.php?v=info");
			List<NameValuePair> nvps3 = new ArrayList<NameValuePair>();

			nvps3.add(new BasicNameValuePair("v", "info"));
			nvps3.add(new BasicNameValuePair("profile_id", uid));
			httpost.setEntity(new UrlEncodedFormEntity(nvps3, HTTP.UTF_8));
			// execute postMethod
			postResponse = httpclient.execute(httpost);
			entity = postResponse.getEntity();
			// System.out.println("Facebook: facebookPostMethod: " +
			// postResponse.getStatusLine());
			if (entity != null) {
				responseStr = EntityUtils.toString(entity);
				// writefile(responseStr);
				entity.consumeContent();
			}
			System.out.println("Facebook: Post Method done("
					+ postResponse.getStatusLine().getStatusCode()
					+ "), response string length: "
					+ (responseStr == null ? 0 : responseStr.length()));
			//System.out.println(statusPair.getValue());

		} catch (Exception e) {
			e.printStackTrace(); // TODO: handle exception
		}
	}
	
		
}
